function Request(baseUrl = '', time = 1000) {

    this.baseUrl = baseUrl;

    this.timer = null;

    function request(url, option = {}) {
        const _this = this;
        return new Promise(async (resolve, reject) => {
            try {
                const response = await fetch(baseUrl + url, option);
                if (response.status == '200') {
                    const data = await response.json();
                    if (data.code == '200') {
                        resolve(data);
                    } else {
                        reject(data.message || '请求失败');
                    }
                    clearInterval(this.timer);
                } else {
                    clearInterval(this.timer);
                    reject(response);
                }
            } catch (e) {
                clearInterval(this.timer);
                reject(e);
            }
        })
    }

    this.post = function (url, data) {
        return request(url, {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json'
            },
            body: JSON.stringify(data)
        })
    }

    this.get = function (url) {
        return request(url, {
            method: 'GET'
        })
    }
}


Request.createRequest = function (baseUrl) {
    return new Request(baseUrl);
}